﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using SmoLiteApi.Sql;
using System.Data.SqlClient;
using SmoLiteApi.Helper;

namespace SmoLiteApi
{
    /// <summary/>
    public class DatabaseCollection : IEnumerable<Database>
    {
        private Server _server;

        /// <summary/>
        internal DatabaseCollection(Server server)
        {
            this._server = server;
        }

        /// <summary/>
        public Database this[string name]
        {
            get
            {
                // get the list
                List<Database> databases = this.FetchObjects(name);

                // return the data
                if (databases.Count == 0)
                {
                    return null;
                }

                // return the one and only
                return databases[0];
            }
        }

        /// <summary/>
        public IEnumerator<Database> GetEnumerator()
        {
            return this.FetchObjects(null).GetEnumerator();
        }

        /// <summary/>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.FetchObjects(null).GetEnumerator();
        }

        /// <summary/>
        private List<Database> FetchObjects(string name)
        {
            List<Database> databases = new List<Database>();

            DatabaseSelectSql databaseSelectSql = new DatabaseSelectSql();

            // set attributes
            databaseSelectSql.AddObject(new { Name = name });

            using (SqlCommandHelper sqlCommandHelper = SqlCommandHelper.FetchSqlCommand(this._server.FetchSqlConnection(), databaseSelectSql.Render()))
            {
                using (SqlDataReader sqlDataReader = sqlCommandHelper.ExecuteReader())
                {
                    while (sqlDataReader.Read())
                    {
                        Database database = new Database(
                            this._server,
                            sqlDataReader.GetString(sqlDataReader.GetOrdinal(Constant.PARAMETER_NAME)));

                        byte recoveryModel = sqlDataReader.GetByte(sqlDataReader.GetOrdinal(Constant.PARAMETER_RECOVERY_MODEL));

                        // set the reoceveyr model
                        if (recoveryModel == Constant.RECOVERY_MODEL_FULL)
                        {
                            database.RecoveryModel = RecoveryModel.Full;
                        }
                        else if (recoveryModel == Constant.RECOVERY_MODEL_BULK_LOGGED)
                        {
                            database.RecoveryModel = RecoveryModel.Bulk;
                        }
                        else if (recoveryModel == Constant.RECOVERY_MODEL_SIMPLE)
                        {
                            database.RecoveryModel = RecoveryModel.Simple;
                        }

                        // add to the list
                        databases.Add(database);
                    }
                }
            }

            // retun the list
            return databases;
        }
    }
}
